package com.itheima.reggie.filter;

import com.alibaba.fastjson.JSON;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.HashMap;

@Slf4j
@Component
@WebFilter("/*")
public class MyFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //强转
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //需要放行的资源
        String[] loginUrls = {
                "/employee/login",
                "/employee/logout",
                "/backend/**",
                "/front/**",
                "/user/login",
                "/user/sendMsg"
        };
        //判断是否放行
        //ant风格类，比较路径
        AntPathMatcher antPathMatcher = new AntPathMatcher();
        String requestURI = request.getRequestURI();
        for (String loginUrl : loginUrls) {
            //比较路径，返回boolean值
            boolean match = antPathMatcher.match(loginUrl, requestURI);
            //true 放行
            if (match) {
                filterChain.doFilter(request, response);
                return;
            }
        }
        //    判断是否登录，否，重定向登录页面
        //后端session'
        Long loginEmployee = (Long) request.getSession().getAttribute("loginEmployee");
        //前端session
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        //判断
        if (loginEmployee != null || loginUser != null) {
            //判断是否为空，存储那个
            if(loginEmployee != null){ //打印
                log.info("客户端用户{}已登录", request.getSession().getAttribute("loginEmployee"));
                Long id = (Long) request.getSession().getAttribute("loginEmployee");
                //线程储存存储id
                BaseContext.setCurrentId(id);
            }
            if(loginUser != null){
                Long id1 = loginUser.getId();
                log.info("移动端用户已登录，用户id为：{}", id1);
                BaseContext.setCurrentId(id1);
            }
            //放行
            filterChain.doFilter(request, response);
            //    线程储存释放id
            BaseContext.remove();
        } else {
            log.info("客户端用户{}未登录", request.getSession().getAttribute("loginEmployee"));
            String jsonString = JSON.toJSONString(R.error("NOTLOGIN"));
            response.getWriter().write(jsonString);
        }
       /* //糅杂一起
        if (loginEmployee != null || loginUser != null) {
            log.info("客户端用户{}已登录", request.getSession().getAttribute("loginEmployee"));
            log.info("移动端用户已登录，用户id为：{}", id1);
            Long id = (Long) request.getSession().getAttribute("loginEmployee");
            //Long id1 = lofinUser.getId();
            BaseContext.setCurrentId(id);
            BaseContext.setCurrentId(id1);
            filterChain.doFilter(request, response);
        } else {
            log.info("客户端用户{}未登录", request.getSession().getAttribute("loginEmployee"));
            log.info("客户端用户{}未登录", id1);
            String jsonString = JSON.toJSONString(R.error("NOTLOGIN"));
            response.getWriter().write(jsonString);
        }
*/
       /* //单独移动端
        if (loginUser != null) {
            log.info("移动端用户已登录，用户id为：{}", id1);
            //Long id1 = lofinUser.getId();
            BaseContext.setCurrentId(id1);
            filterChain.doFilter(request, response);
        } else {
            log.info("客户端用户{}未登录", id1);
            String jsonString = JSON.toJSONString(R.error("NOTLOGIN"));
            response.getWriter().write(jsonString);
        }*/

    }
}
